/****************************************************************
 * <strong>MFProppertie属性读取类</strong>
 * <p>
 * 属性读取相关方法
 * </p>
 *
 * @author AndyZhang 2011
 * @version 0.1
 * ToDo:
 ***************************************************************/

package com.thinkwide.data.core;

import com.thinkwide.data.debug.MFDebug;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;

public class MFPropperties {
    private static Logger logger = LoggerFactory.getLogger(MFPropperties.class);
    private String defFileName;
    private Hashtable<String, String> keyList;

    /**
     * 自动获取路径
     *
     * @param fileName 文件名称
     * @return 返回路径
     * @throws Throwable
     */
    public static String getPropPath(String fileName) throws Throwable {
        //String pathClass=MFPath.getClassPath();
        //pathClass=pathClass.substring(0,(pathClass.lastIndexOf("/classes") + 1)) + "prop/"+fileName;

        String pathClass = MFPath.getDirFromClassLoader();
        pathClass = pathClass + MFPath.fseparator + "prop" + MFPath.fseparator + fileName;

        logger.debug(pathClass);
        if (!MFPath.isExists(pathClass)) {
            MFDebug.println("not found file:" + pathClass + ",auto create file");
        }
        return pathClass;

    }

    public static boolean isExists(String fileName) throws Throwable {
        String path = MFPropperties.getPropPathExt(fileName);
        return MFPath.isExists(path);
    }

    /**
     * 自动获取路径
     *
     * @param fileName 文件名称
     * @return 返回路径
     * @throws Throwable
     */
    public static String getPropPathExt(String fileName) throws Throwable {
        String pathClass = MFPath.getDirFromClassLoader();
        pathClass = pathClass + MFPath.fseparator + "prop" + MFPath.fseparator + fileName;

        return pathClass;

    }

    /**
     * 获取属性的值
     *
     * @param fpath 完整路径
     * @param key   键值
     * @return
     * @throws Throwable
     */
    public static String getPropValue(String fpath, String key) throws Throwable {
        Properties prop = new Properties();
        try {

            InputStream in = new BufferedInputStream(new FileInputStream(fpath));
            prop.load(in);
            in.close();
            return prop.getProperty(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 获取属性的值
     *
     * @param fName 文件名称
     * @param key
     * @return
     * @throws Throwable
     */
    public static String getPropValueExt(String fName, String key) throws Throwable {
        return getPropValue(getPropPath(fName), key);
    }


    /**
     * 设置属性的值
     *
     * @param fpath 完整路径
     * @param key
     * @param value
     */
    public static void setPropValue(String fpath, String key, String value) {
        Properties prop = new Properties();
        try {
            InputStream fis = new FileInputStream(fpath);
            prop.load(fis);

            OutputStream fos = new FileOutputStream(fpath);
            prop.setProperty(key, value);

            prop.store(fos, "Update '" + key + "' value");
            fos.close();
            fis.close();
        } catch (IOException e) {
            System.err.println("Visit " + fpath + " for updating " + key + " value error");
        }

    }

    /**
     * 设置属性值
     *
     * @param fName 文件名称
     * @param key   键值
     * @param value
     * @throws Throwable
     */
    public static void setPropValueExt(String fName, String key, String value) throws Throwable {
        setPropValue(getPropPath(fName), key, value);
    }

    /**
     * 构造函数
     *
     * @param fileName 文件名称不包含路径
     * @param read     true为直接加载
     * @throws Throwable
     */
    public MFPropperties(String fileName, boolean read) throws Throwable {
        defFileName = fileName;
        keyList = new Hashtable<String, String>();
        if (read) loadData();

    }

    /**
     * 构造函数
     *
     * @throws Throwable
     */
    public MFPropperties() throws Throwable {
        keyList = new Hashtable<String, String>();

    }


    /**
     * 加载数据
     *
     * @throws Throwable
     */
    public void saveData(String fpath) throws Throwable {

        Properties prop = new Properties();
        try {
            //InputStream fis = new FileInputStream(fpath);
            //prop.load(fis);

            OutputStream fos = new FileOutputStream(fpath);
            for (Map.Entry<String, String> item : keyList.entrySet()) {
                prop.setProperty(item.getKey(), item.getValue());
            }
            prop.store(fos, "Update ");
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 加载数据
     *
     * @throws Throwable
     */
    public void saveData() throws Throwable {
        saveData(MFPropperties.getPropPath(defFileName));

    }

    /**
     * 加载数据
     *
     * @throws Throwable
     */
    public void loadData(String fpath) throws Throwable {
        keyList.clear();
        Properties prop = new Properties();
        if (MFPath.isExists(fpath))
            try {
                InputStream in = new BufferedInputStream(new FileInputStream(fpath));
                prop.load(in);
                Enumeration<?> list = prop.keys();
                while (list.hasMoreElements()) {
                    String key = (String) list.nextElement();
                    keyList.put(key, prop.getProperty(key));
                }
                in.close();
            } catch (Exception e) {
                e.printStackTrace();

            }

    }

    /**
     * 加载数据
     *
     * @throws Throwable
     */
    public void loadData() throws Throwable {
        loadData(MFPropperties.getPropPath(defFileName));

    }

    public String getPropValue(String key) {
        String value = keyList.get(key);
        return value;

    }

    public boolean containsKey(String key) {
        return keyList.containsKey(key);
    }

    public void setPropValue(String key, String value) {

        if (keyList.containsKey(key)) keyList.remove(key);

        keyList.put(key, value);

    }

    public Hashtable<String, String> getkeyList() {
        return keyList;
    }

}
